import requests
from lxml import etree
from openpyxl import Workbook
import re
import random

cookie_list = ['cy=2; cye=beijing; _lxsdk_cuid=176ae754e66c8-04b434357e9be2-c791e37-1fa400-176ae754e67c8; _lxsdk=176ae754e66c8-04b434357e9be2-c791e37-1fa400-176ae754e67c8; _hc.v=2e2afee7-fed0-d0cb-090d-85f83de210a2.1609244692; s_ViewType=10; _dp.ac.v=8d001faa-d611-48f7-a457-6d362b86c220; ctu=08ee6d98541bc4eb6bb2a236500d26b33a14579d1e2b7ba2670891f3fb23d0c4; aburl=1; fspop=test; _lx_utm=utm_source%3DBaidu%26utm_medium%3Dorganic; Hm_lvt_602b80cf8079ae6591966cc70a3940e7=1610176608,1610192375,1610247709,1610931617; lgtoken=098cf7a01-fde8-4908-92bc-2eb4e786c154; dper=893c929e179027e6f4eb10b93fbd9bc54e6c174f23790c656f663bfa1591f869d6603054ab2a5e262d500c938b787917098f26b7ddc3d210f3c9074f38b7c4fa49a0932a3ea34dcfc86dc5192d987fb108adb8a3c71b4aac5b04153c2736df70; ll=7fd06e815b796be3df069dec7836c3df; ua=16720511699; dplet=ee2bb6cde1fbfbed9a5f08fe94872a22; Hm_lpvt_602b80cf8079ae6591966cc70a3940e7=1610931732; _lxsdk_s=1771301b95d-ce8-a66-ed2%7C%7C272',
               'cy=2; cye=beijing; _lxsdk_cuid=176ae754e66c8-04b434357e9be2-c791e37-1fa400-176ae754e67c8; _lxsdk=176ae754e66c8-04b434357e9be2-c791e37-1fa400-176ae754e67c8; _hc.v=2e2afee7-fed0-d0cb-090d-85f83de210a2.1609244692; s_ViewType=10; _dp.ac.v=8d001faa-d611-48f7-a457-6d362b86c220; ctu=08ee6d98541bc4eb6bb2a236500d26b33a14579d1e2b7ba2670891f3fb23d0c4; aburl=1; fspop=test; cityid=2; default_ab=map%3AA%3A1; _lx_utm=utm_source%3DBaidu%26utm_medium%3Dorganic; Hm_lvt_602b80cf8079ae6591966cc70a3940e7=1610247709,1610931617,1610968506,1611018326; lgtoken=05e386ff3-74fb-4c04-8cbe-637c628b8cee; dplet=1b0f00ad35756519f7394419bf9ceaab; dper=893c929e179027e6f4eb10b93fbd9bc5528d7ff7f060d85e945d7938bcd52e52304f1890d68dcf868077b45c878f211cd269dc580599fe8d5d7e2662dfa5b1a41c0b978891cfd9df0e2124cd4e41f015e72d483493ca0d6dcc58cb930f312070; ll=7fd06e815b796be3df069dec7836c3df; ua=16720511699; Hm_lpvt_602b80cf8079ae6591966cc70a3940e7=1611018384; _lxsdk_s=177182ccf3c-53a-cb6-16d%7C%7C13',
               'cy=2; cye=beijing; _lxsdk_cuid=176ae754e66c8-04b434357e9be2-c791e37-1fa400-176ae754e67c8; _lxsdk=176ae754e66c8-04b434357e9be2-c791e37-1fa400-176ae754e67c8; _hc.v=2e2afee7-fed0-d0cb-090d-85f83de210a2.1609244692; s_ViewType=10; _dp.ac.v=8d001faa-d611-48f7-a457-6d362b86c220; ctu=08ee6d98541bc4eb6bb2a236500d26b33a14579d1e2b7ba2670891f3fb23d0c4; aburl=1; fspop=test; cityid=2; default_ab=map%3AA%3A1; _lx_utm=utm_source%3DBaidu%26utm_medium%3Dorganic; Hm_lvt_602b80cf8079ae6591966cc70a3940e7=1610247709,1610931617,1610968506,1611018326; lgtoken=080b61c0f-8b29-48ec-bc5d-e6b624027392; dplet=d1341226ceead17749f19f44d64c3713; dper=cb9664133a13483ca6cd60497236cba49fb308e5a12e097780a5c22493c3ca3e4131a7602ea048362dcf689dfb84ee5dd4c59071d6b33f4ca85f03a6528bf07d586c98cf241f72c7970816328dacca0a3ffd9ed76ac22158aba59de8a195f8ac; ll=7fd06e815b796be3df069dec7836c3df; ua=17053805906; Hm_lpvt_602b80cf8079ae6591966cc70a3940e7=1611018482; _lxsdk_s=177182ccf3c-53a-cb6-16d%7C%7C33',
               'cy=2; cye=beijing; _lxsdk_cuid=176ae754e66c8-04b434357e9be2-c791e37-1fa400-176ae754e67c8; _lxsdk=176ae754e66c8-04b434357e9be2-c791e37-1fa400-176ae754e67c8; _hc.v=2e2afee7-fed0-d0cb-090d-85f83de210a2.1609244692; s_ViewType=10; _dp.ac.v=8d001faa-d611-48f7-a457-6d362b86c220; ctu=08ee6d98541bc4eb6bb2a236500d26b33a14579d1e2b7ba2670891f3fb23d0c4; aburl=1; fspop=test; cityid=2; default_ab=map%3AA%3A1; _lx_utm=utm_source%3DBaidu%26utm_medium%3Dorganic; Hm_lvt_602b80cf8079ae6591966cc70a3940e7=1610247709,1610931617,1610968506,1611018326; lgtoken=0787c0096-86c8-4010-b33a-c53eb9cbc404; dplet=ef89376397a3a820ed30ca75e65fbd71; dper=d0ed85db2509a8eb5e4baf2241746456a4714107b3aa1760c9aca4cc38d9f28c7090cf9905fe4bf55157b2cfeace4f315b763257d644f2a04af398b74216b85706d0425631566f8df90b9dbd777298f5ce21c50f777843a8a3607a715611445f; ll=7fd06e815b796be3df069dec7836c3df; ua=17053805897; Hm_lpvt_602b80cf8079ae6591966cc70a3940e7=1611018640; _lxsdk_s=177182ccf3c-53a-cb6-16d%7C%7C59',
               'cy=2; cye=beijing; _lxsdk_cuid=176ae754e66c8-04b434357e9be2-c791e37-1fa400-176ae754e67c8; _lxsdk=176ae754e66c8-04b434357e9be2-c791e37-1fa400-176ae754e67c8; _hc.v=2e2afee7-fed0-d0cb-090d-85f83de210a2.1609244692; s_ViewType=10; _dp.ac.v=8d001faa-d611-48f7-a457-6d362b86c220; ctu=08ee6d98541bc4eb6bb2a236500d26b33a14579d1e2b7ba2670891f3fb23d0c4; aburl=1; fspop=test; cityid=2; default_ab=map%3AA%3A1; _lx_utm=utm_source%3DBaidu%26utm_medium%3Dorganic; Hm_lvt_602b80cf8079ae6591966cc70a3940e7=1610247709,1610931617,1610968506,1611018326; lgtoken=043fef0a6-1011-4f1d-ac4f-2c3b402b9874; dplet=c23ef43f3d13a2afa33dfb8ed84dca26; dper=7ff20d2d1f307fa54304db08643e1f6b45f31d0cb592fa4bc60077faf7a33d84247b7c225aa984d93da69d9c6fa01f28e836d69971a75803378530adfda46fe19e698a23a99c735c1e4893c4b4cf6fe0dbf8263810ba6347ef8bc9092553bfdf; ll=7fd06e815b796be3df069dec7836c3df; ua=17061190697; Hm_lpvt_602b80cf8079ae6591966cc70a3940e7=1611018728; _lxsdk_s=177182ccf3c-53a-cb6-16d%7C%7C81',
               'cy=2; cye=beijing; _lxsdk_cuid=176ae754e66c8-04b434357e9be2-c791e37-1fa400-176ae754e67c8; _lxsdk=176ae754e66c8-04b434357e9be2-c791e37-1fa400-176ae754e67c8; _hc.v=2e2afee7-fed0-d0cb-090d-85f83de210a2.1609244692; s_ViewType=10; _dp.ac.v=8d001faa-d611-48f7-a457-6d362b86c220; ctu=08ee6d98541bc4eb6bb2a236500d26b33a14579d1e2b7ba2670891f3fb23d0c4; aburl=1; fspop=test; cityid=2; default_ab=map%3AA%3A1; _lx_utm=utm_source%3DBaidu%26utm_medium%3Dorganic; Hm_lvt_602b80cf8079ae6591966cc70a3940e7=1610247709,1610931617,1610968506,1611018326; lgtoken=09156661b-9771-4864-a1f1-48d51e8615b5; dplet=217b2d5ea3640c0f4ae5ad3e2ebcd71b; dper=c09030f420ad61a7cffccc5c72514092a28099769694c0447c0d6b7b8074e57af3974fd98087a7876443e19aba9044fb270bdcdc3378c263c37edf9ebc2f7fcff841a5285348c9bfec00add433658d8b8fd7aff5149a4972f427bdd061f2ca6b; ll=7fd06e815b796be3df069dec7836c3df; ua=17061215957; Hm_lpvt_602b80cf8079ae6591966cc70a3940e7=1611018808; _lxsdk_s=177182ccf3c-53a-cb6-16d%7C%7C105',
               'cy=2; cye=beijing; _lxsdk_cuid=176ae754e66c8-04b434357e9be2-c791e37-1fa400-176ae754e67c8; _lxsdk=176ae754e66c8-04b434357e9be2-c791e37-1fa400-176ae754e67c8; _hc.v=2e2afee7-fed0-d0cb-090d-85f83de210a2.1609244692; s_ViewType=10; _dp.ac.v=8d001faa-d611-48f7-a457-6d362b86c220; ctu=08ee6d98541bc4eb6bb2a236500d26b33a14579d1e2b7ba2670891f3fb23d0c4; aburl=1; fspop=test; cityid=2; default_ab=map%3AA%3A1; _lx_utm=utm_source%3DBaidu%26utm_medium%3Dorganic; Hm_lvt_602b80cf8079ae6591966cc70a3940e7=1610247709,1610931617,1610968506,1611018326; lgtoken=02522bdb5-a076-470d-bbba-db5a50c90429; dplet=83bebfc0877ef9a6d5d8386760799e84; dper=8c6f34d05854e64007c14095de8531f86ce3f9523399d3cfec0f797ff3b00a13b6b95f8bc97f8220d951447a1704383b361f321500f1fcb7562a751905c6e000c8afa33844f942f8a9b31a5b4ebe3e6d72eb02de2c184a06b1a0a230064926b3; ll=7fd06e815b796be3df069dec7836c3df; ua=17065108244; Hm_lpvt_602b80cf8079ae6591966cc70a3940e7=1611018878; _lxsdk_s=177182ccf3c-53a-cb6-16d%7C%7C125',
               'cy=2; cye=beijing; _lxsdk_cuid=176ae754e66c8-04b434357e9be2-c791e37-1fa400-176ae754e67c8; _lxsdk=176ae754e66c8-04b434357e9be2-c791e37-1fa400-176ae754e67c8; _hc.v=2e2afee7-fed0-d0cb-090d-85f83de210a2.1609244692; s_ViewType=10; _dp.ac.v=8d001faa-d611-48f7-a457-6d362b86c220; ctu=08ee6d98541bc4eb6bb2a236500d26b33a14579d1e2b7ba2670891f3fb23d0c4; aburl=1; fspop=test; cityid=2; default_ab=map%3AA%3A1; _lx_utm=utm_source%3DBaidu%26utm_medium%3Dorganic; Hm_lvt_602b80cf8079ae6591966cc70a3940e7=1610247709,1610931617,1610968506,1611018326; lgtoken=07faaba1c-5f27-4595-810f-a2a6d52c1c35; dplet=4801a036527ca96ae637f4495e1f2416; dper=5e942d16fe5b216324a7ed56372f2cbdb72652e96560b15f9d2b4f4846f9abc1a1d17e94181fac525d82e396a154308ba6bcceab0ad60a52a5944c18536944101f166b2b414c11b0657c972ebbd4878e1f3b0045efbfd604b1a5ba842860491d; ll=7fd06e815b796be3df069dec7836c3df; ua=17061190231; Hm_lpvt_602b80cf8079ae6591966cc70a3940e7=1611018937; _lxsdk_s=177182ccf3c-53a-cb6-16d%7C%7C145',
               'cy=2; cye=beijing; _lxsdk_cuid=176ae754e66c8-04b434357e9be2-c791e37-1fa400-176ae754e67c8; _lxsdk=176ae754e66c8-04b434357e9be2-c791e37-1fa400-176ae754e67c8; _hc.v=2e2afee7-fed0-d0cb-090d-85f83de210a2.1609244692; s_ViewType=10; _dp.ac.v=8d001faa-d611-48f7-a457-6d362b86c220; ctu=08ee6d98541bc4eb6bb2a236500d26b33a14579d1e2b7ba2670891f3fb23d0c4; aburl=1; fspop=test; cityid=2; default_ab=map%3AA%3A1; Hm_lvt_602b80cf8079ae6591966cc70a3940e7=1610247709,1610931617,1610968506,1611018326; _lx_utm=utm_source%3DBaidu%26utm_medium%3Dorganic; lgtoken=0be31b0b2-96bc-40ee-ba30-feb480795048; dplet=d2bedcd01e70bbca5ec548a41c732843; dper=dc1f3ef0decc35d836984f9db0a797da08688f05cd9bd3f99dfb755aabe7de9e5b9b702e05bbc44f0717775bcc1503a51c7b5763cde453eed8ea53cbf3f355b387d15598c4de8cecdd75c723ce185289229c310000f3b51d51437bd8004d807e; ll=7fd06e815b796be3df069dec7836c3df; ua=17061223685; Hm_lpvt_602b80cf8079ae6591966cc70a3940e7=1611019002; _lxsdk_s=177182ccf3c-53a-cb6-16d%7C%7C169',
               'cy=2; cye=beijing; _lxsdk_cuid=176ae754e66c8-04b434357e9be2-c791e37-1fa400-176ae754e67c8; _lxsdk=176ae754e66c8-04b434357e9be2-c791e37-1fa400-176ae754e67c8; _hc.v=2e2afee7-fed0-d0cb-090d-85f83de210a2.1609244692; s_ViewType=10; _dp.ac.v=8d001faa-d611-48f7-a457-6d362b86c220; ctu=08ee6d98541bc4eb6bb2a236500d26b33a14579d1e2b7ba2670891f3fb23d0c4; aburl=1; fspop=test; cityid=2; default_ab=map%3AA%3A1; Hm_lvt_602b80cf8079ae6591966cc70a3940e7=1610247709,1610931617,1610968506,1611018326; _lx_utm=utm_source%3DBaidu%26utm_medium%3Dorganic; lgtoken=0958e6bef-096f-4995-be6a-9bb667f90925; dplet=e88ad49d93d42885ff4516e6d2aa5ce6; dper=a0060bf0966d9cddfe4b0e82b7918388de08af3d38ae6db4acc2783d368497ea127127e91766fdfc62e8a3d85d95cd2e5c2ff93bc3e1e402d408bd93a1429a653bc09c06a788fea09b012add51e1d3aa9cea63a585aa07b7682517088b756468; ll=7fd06e815b796be3df069dec7836c3df; ua=17060346002; Hm_lpvt_602b80cf8079ae6591966cc70a3940e7=1611019072; _lxsdk_s=177182ccf3c-53a-cb6-16d%7C%7C189'
               ]
patten = re.compile(r'textLength="(.*?)"')
work = Workbook()
w = work.active
w.append(['店铺名称', '评价条数', '消费', '产品', '环境', '服务', '全部好评', '经纬度'])
headers = {
    'Referer': 'https://www.dianping.com/',
    'Host': 'www.dianping.com',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36',
    'Cookie': 'cy=2; cye=beijing; _lxsdk_cuid=176ae754e66c8-04b434357e9be2-c791e37-1fa400-176ae754e67c8; _lxsdk=176ae754e66c8-04b434357e9be2-c791e37-1fa400-176ae754e67c8; _hc.v=2e2afee7-fed0-d0cb-090d-85f83de210a2.1609244692; s_ViewType=10; _dp.ac.v=8d001faa-d611-48f7-a457-6d362b86c220; ctu=08ee6d98541bc4eb6bb2a236500d26b33a14579d1e2b7ba2670891f3fb23d0c4; aburl=1; fspop=test; _lx_utm=utm_source%3DBaidu%26utm_medium%3Dorganic; Hm_lvt_602b80cf8079ae6591966cc70a3940e7=1610176608,1610192375,1610247709,1610931617; lgtoken=098cf7a01-fde8-4908-92bc-2eb4e786c154; dper=893c929e179027e6f4eb10b93fbd9bc54e6c174f23790c656f663bfa1591f869d6603054ab2a5e262d500c938b787917098f26b7ddc3d210f3c9074f38b7c4fa49a0932a3ea34dcfc86dc5192d987fb108adb8a3c71b4aac5b04153c2736df70; ll=7fd06e815b796be3df069dec7836c3df; ua=16720511699; dplet=ee2bb6cde1fbfbed9a5f08fe94872a22; Hm_lpvt_602b80cf8079ae6591966cc70a3940e7=1610931732; _lxsdk_s=1771301b95d-ce8-a66-ed2%7C%7C272'
}

url_svg = 'http://s3plus.meituan.net/v1/mss_0a06a471f9514fc79c981b5466f56b91/svgtextcss/7f743a72bb6ae6e6164a6e33c7a38fc8.svg'
svg_resp = requests.get(url=url_svg).text
length = patten.findall(svg_resp)
url_css = 'http://s3plus.meituan.net/v1/mss_0a06a471f9514fc79c981b5466f56b91/svgtextcss/ea4ee0f1607fa4ca7d8c3e116a267556.css'
css_resp = requests.get(url=url_css).text


def get_all_url():
    start_urls = ['https://www.dianping.com/search/keyword/2/0_%E4%BE%BF%E5%88%A9%E8%9C%82',
                  'https://www.dianping.com/search/keyword/2/0_711',
                  'https://www.dianping.com/search/keyword/2/0_%E5%85%A8%E5%AE%B6',
                  'https://www.dianping.com/search/keyword/2/0_%E7%BD%97%E6%A3%AE'
                  ]

    for url in start_urls:
        response = requests.get(url=url, headers=headers).text
        html = etree.HTML(response)
        url_list = html.xpath('//li[@class=""]/div[@class="pic"]/a/@href')
        return url_list


def get_detail(url_list):
    for i in url_list:
        headers['Cookie'] = random.choice()
        # 好评页面
        review_url = i + '/review_all?queryType=reviewGrade&queryVal=good'
        # 位置信息页面
        position_url = i.replace('www', 'm') + '/map'
        response = requests.get(url=review_url, headers=headers).text
        position_response = requests.get(url=position_url).text
        html = etree.HTML(response)
        css_name_list = html.xpath('//svgmtsi/@class')
        css = css_resp.replace('\n', '').replace(' ', '')
        p_patten = re.compile('"shopLat":(.*?),"shopLng":(.*?),')
        position = '|'.join(p_patten.findall(position_response)[0])
        message_list = trance_chart(css_name_list, css, response)
        message_list.append(position)
        w.append(message_list)
        work.save('大众点评.xlsx')


def trance_chart(css_name_list, css, response):
    for css_name in css_name_list:
        pattern = re.compile(r'\.%s{background:-(\d+).0px-(\d+).0px;}' % css_name)
        coord = pattern.findall(css)
        if coord:
            x, y = coord[0]
            x, y = int(x), int(y)
            num_patten = re.compile('id="(.*?)" d="M0 (.*?) H')
            number_list = num_patten.findall(svg_resp)
            axis_y = [(i, j) for i, j in number_list if y <= int(j)][0]
            chart_patten = re.compile(r'href="#%s".*>(.*?)<' % axis_y[0])
            chart = chart_patten.findall(svg_resp)[0][x // 14]
            response = response.replace(f'<svgmtsi class="{css_name}"></svgmtsi>', chart)
            response = response.replace(f'<bb class="{css_name}"></bb>', chart)
    html = etree.HTML(response)
    title = html.xpath('//*[@id="review-list"]/div[2]/div[1]/div[1]/h1/text()')[0]
    evaluate_number = html.xpath('//*[@id="review-list"]/div[2]/div[1]/div[2]/span[1]/text()')[0]
    avg = html.xpath('//*[@id="review-list"]/div[2]/div[1]/div[2]/span[2]/text()')[0]
    product = html.xpath('//*[@id="review-list"]/div[2]/div[1]/div[2]/span[3]/span[1]/text()')[0]
    et = html.xpath('//*[@id="review-list"]/div[2]/div[1]/div[2]/span[3]/span[2]/text()')[0]
    fw = html.xpath('//*[@id="review-list"]/div[2]/div[1]/div[2]/span[3]/span[3]/text()')[0]
    element_list = [i.strip() for i in ''.join(
        [i.strip('\n').replace('\n', '') for i in html.xpath('//div[@class="review-words Hide"]/text()')]).split('\t')]
    message_list = [title, evaluate_number, avg, product, et, fw, ''.join(element_list)]
    return message_list


if __name__ == '__main__':
    url_list = get_all_url()
    get_detail(url_list)
